﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows;

namespace for7raid.vk
{
    /*   <user>
    <uid>1</uid>
    <first_name>Павел</first_name>
    <last_name>Дуров</last_name>
    <photo>http://cs109.vkontakte.ru/u00001/c_df2abf56.jpg</photo>
    <online>0</online>
    <lists list="true">
     <lid>2</lid>
     <lid>3</lid>
    </lists>
   </user>
     * 
     * first_name, last_name, nickname, sex, bdate (birthdate), city, country, timezone, photo, photo_medium, photo_big, online, lists, domain
  */
    public class User : DependencyObject
    {
        public static User Me { get; set; }
        public static int MeId { get { return Me != null ? Me.uid : 0; } }

        public int uid { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Photo { get; set; }
        public string City { get; set; }
        public string Birthdate { get; set; }
        public string Education { get; set; }

        public string FullName { get { return FirstName + " " + LastName; } }

        public Uri Uri { get { return new Uri("http://vk.com/id" + uid,UriKind.Absolute); } }

        public static List<User> FromResponse(XmlDocument doc)
        {

            return doc.SelectNodes("/response/user").OfType<XmlNode>().Select(i => new User()
            {
                uid = int.Parse(i["uid"].InnerText),

                FirstName = i["first_name"].InnerText.Encode(),
                LastName = i["last_name"].InnerText.Encode(),
                Photo = i["photo"].InnerText,
                City = i["city"] != null ?  i["city"].InnerText:"",
                Birthdate = i["bdate"] != null ? i["bdate"].InnerText : "",
                Education = i["university_name"] != null ? i["university_name"].InnerText : ""
            }).OrderBy(i => i.FirstName).ToList();
        }

        public static User MeFromResponse(XmlDocument doc)
        {
            var node = doc.SelectSingleNode("/response");

            return new User()
            {
                uid = node["user_id"].ParseXmlNodeAsInt(),
                FirstName = node["user_name"].InnerText
            };
        }

        public static User MeFromResponseEx(XmlDocument doc)
        {
            var node = doc.SelectSingleNode("/response");

            return new User()
            {
                uid = node["user_id"].ParseXmlNodeAsInt(),
                FirstName = node["user_name"].InnerText,
                Photo= node["user_photo"].InnerText,
                City = doc["user_city"] != null ? doc["user_city"].InnerText : ""
            };
        }

        public class Comparer : IEqualityComparer<User>
        {

           

            public bool Equals(User x, User y)
            {
                return x.uid == y.uid;
            }

            public int GetHashCode(User obj)
            {
                return obj.uid;
            }
        }
    }


}
